#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()<FTPServerDelegate>

#ifdef DEBUG
@property (nonatomic, retain) FtpServer *theServer;
@property (nonatomic, copy) NSString *baseDir;

@property(nonatomic,assign)pthread_mutex_t serverMutex;
@property(nonatomic,assign)pthread_mutex_t BSSIDMutex;

@property(nonatomic,assign)BOOL isAnonymous;
@property(nonatomic,retain)NSString* userName;
@property(nonatomic,retain)NSString* userPwd;
@property(nonatomic,assign) unsigned int ftpPort;
#endif

@end

@implementation AppDelegate

#ifdef DEBUG
@synthesize baseDir, theServer;
#endif

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    ViewController *viewController = [[ViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
    
    [self.window setRootViewController:nav];
    
    //ftp server
#ifdef DEBUG
    NSArray *docFolders = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
    self.baseDir = docFolders.lastObject;
    self.ftpPort = SERVER_PORT;
    pthread_mutex_init(&_serverMutex, NULL);
    self.userName = @"";
    self.userPwd = @"";
    self.isAnonymous = YES;
    
    [self startFtpServer];
#endif
    
    return [super application:application didFinishLaunchingWithOptions:launchOptions];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
    
    return [super application:application handleOpenURL:url] ;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    [super applicationWillResignActive:application];
    
#ifdef DEBUG
    [self stopFtpServer];
#endif
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    [super applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    [super applicationWillEnterForeground:application];
    
    
#ifdef DEBUG
    [self startFtpServer];
#endif
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    [super applicationDidBecomeActive:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    [super applicationWillTerminate:application];
}


#pragma mark ftp server

#ifdef DEBUG

- (FtpServer *)getServer {
    FtpServer *ret = nil;
    pthread_mutex_lock(&_serverMutex);
    ret = theServer;
    pthread_mutex_unlock(&_serverMutex);
    return ret;
}

- (void)setServer:(FtpServer *)server {
    pthread_mutex_lock(&_serverMutex);
    self.theServer = server;
    pthread_mutex_unlock(&_serverMutex);
}

- (void)startFtpServer {
    if (![self getServer]) {
        FtpServer *aServer = [[FtpServer alloc] initWithPort:self.ftpPort withDir:self.baseDir notifyObject:self];
        [self setServer:aServer];
        aServer.clientEncoding = NSUTF8StringEncoding;
        NSLog(@"ftp server start ");
    }
}

- (void)stopFtpServer {
    if ([self getServer]) {
        
        [theServer stopFtpServer];
        theServer = nil;
    }
}

- (void)didReceiveFileListChanged {
    NSLog(@"didReceiveFileListChanged --" );
    [[HybridEventBus defaultEventBus] postData:@{} Tag:@"ftpReceiveFileListChanged"];
}

- (void)clientDisconnect {
    [[NSURLCache sharedURLCache]removeAllCachedResponses];
    NSLog(@"closeConnection --" );
    [[HybridEventBus defaultEventBus] postData:@{} Tag:@"ftpCloseConnection"];
}
#endif

@end
